//
//  YWDownloadCell.m
//  videoTest3
//
//  Created by jun peng on 2022/7/20.
//

#import "YWDownloadCell.h"

@interface YWDownloadCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIProgressView *progressView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *situationBtnConstraintWidth;
@property (weak, nonatomic) IBOutlet UIButton *situationBtn;

- (IBAction)situationBtnClick:(UIButton *)sender;



@end

@implementation YWDownloadCell


+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"YWDownloadCell";
    YWDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        // 从xib中加载cell
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YWDownloadCell" owner:nil options:nil] lastObject];
    }
    return cell;
}

- (void)setStatus:(YWDownloadStatus *)status{
    _status = status;
    
    if (![status.PicUrl isKindOfClass:[NSNull class]] && status.PicUrl != nil) {
        
        [self.iconView sd_setImageWithURL:[NSURL URLWithString:status.PicUrl] placeholderImage:[UIImage imageNamed:@""]];
        
    }
    else{
        
        [self.iconView sd_setImageWithURL:[NSURL URLWithString:@""] placeholderImage:[UIImage imageNamed:@""]];
        
    }
    
    self.titleLabel.text = status.Title;
    
    self.situationBtn.userInteractionEnabled = YES;
    switch (status.downloadType) {
        case YWDownloadType_Waiting:
        {
            self.progressView.hidden = NO;
            self.situationBtn.hidden = NO;
            self.situationBtnConstraintWidth.constant = 60;
            
            [self.situationBtn setTitle:@"等待中" forState:UIControlStateNormal];
            
            [self.situationBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        }
            break;
        case YWDownloadType_Downloading:
        {
            self.progressView.hidden = NO;
            self.situationBtn.hidden = NO;
            self.situationBtnConstraintWidth.constant = 60;
            
            [self.situationBtn setTitle:@"下载中" forState:UIControlStateNormal];
            
            [self.situationBtn setTitleColor:mainColor forState:UIControlStateNormal];
        }
            break;
        case YWDownloadType_Pause:
        {
            self.progressView.hidden = NO;
            self.situationBtn.hidden = NO;
            self.situationBtnConstraintWidth.constant = 60;
            
            [self.situationBtn setTitle:@"已暂停" forState:UIControlStateNormal];
            
            [self.situationBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        }
            break;
        case YWDownloadType_Finish:
        {
            self.progressView.hidden = YES;
            self.situationBtn.hidden = YES;
            self.situationBtnConstraintWidth.constant = 0;
            
            
            
        }
            break;
        case YWDownloadType_Error:
        {
            self.progressView.hidden = NO;
            self.situationBtn.hidden = NO;
            self.situationBtnConstraintWidth.constant = 60;
            
            [self.situationBtn setTitle:@"下载失败" forState:UIControlStateNormal];
            self.situationBtn.userInteractionEnabled = NO;
            
            
            [self.situationBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            
        }
            break;
            
        default:  //remove
        {
            self.progressView.hidden = YES;
            self.situationBtn.hidden = YES;
            self.situationBtnConstraintWidth.constant = 0;
            
        }
            break;
    }
    
    
    
    
    
    
    [self.progressView setProgress:status.currentSize*1.0/status.totalSize];
    
    
    
    
    
}



- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.iconView.layer.cornerRadius = 5;
    self.iconView.clipsToBounds = YES;
    self.situationBtn.layer.cornerRadius = 12;
    
    
//         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ceshi:) name:@"ceshi" object:nil];
}

//-(void)ceshi:(NSNotification *)notification{
//    
//    //完成任务就要从字典移除模型
//    
//    //拿出模型
//    NSNumber *sizeNum = notification.userInfo[@"size"];
//    CGFloat size = sizeNum.floatValue;
//    NSString *url = notification.userInfo[@"url"];
//    NSNumber *currentSizeNum = notification.userInfo[@"currentSizeNum"];
//    
//    if ([self.status.VideoUrl isEqualToString:url]) {
//        
//        self.status.currentSize = currentSizeNum.intValue;
//        
//        [self.progressView setProgress:size];
//        
//    }
//}
//
//-(void)dealloc{
//    
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//    
//}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
}


- (IBAction)situationBtnClick:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(downloadCell:didClickSituationBtnWithStatus:)]) {
        [self.delegate downloadCell:self didClickSituationBtnWithStatus:self.status];
    }
}
@end
